// Copyright (c) 2008-2010, Arne Claus
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
// following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following 
//   disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
//   disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name Arne Claus nor the names of any contributors may be used to endorse or promote products
//   derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef __INCLUDED_CRL_PARAMETER_ATOMIC_H_
#define __INCLUDED_CRL_PARAMETER_ATOMIC_H_

#include "CBL_Tuple.h"
#include "CBL_Matrix.h"
#include "CBL_ResourcePtr.h"

CB_SUBNAMESPACE_START(RenderLib)

// ---------------------------------------------------------------------------------------------------------------------
//	IParameterAtomic
// ---------------------------------------------------------------------------------------------------------------------

class IParameterAtomic : public IResource
{
public:
	
	enum EValueType
	{
		MatrixType = 0,
		FloatType,
		IntType,
		BoolType
	};
	
public:
	
	IParameterAtomic(EValueType _ValueType, uint16 _NumRegisters=1);
	virtual ~IParameterAtomic() {}
	
	EValueType GetValueType() const	{ return m_ValueType; }
	uint16 GetNumRegisters() const	{ return m_NumRegisters; }
    
    virtual int Bind() = 0;
	
protected:
	
	uint16			m_NumRegisters;
	EValueType		m_ValueType;
};

// ---------------------------------------------------------------------------------------------------------------------
//	IMatrix4x4ParameterAtomic
// ---------------------------------------------------------------------------------------------------------------------

class IMatrix4x4ParameterAtomic : public IParameterAtomic
{
public:
	
	IMatrix4x4ParameterAtomic(uint16 _NumMatrices=1);
	virtual ~IMatrix4x4ParameterAtomic();
	
	CB_FORCEINLINE void CB_INLINE_ATTR SetValue(Math::Float4x4& _rValue, uint16 _Index=0);
	CB_FORCEINLINE void CB_INLINE_ATTR SetValues(Math::Float4x4* _pValues, uint16 _Count=1);
	
	const Math::Float4x4* GetValues() const { return m_ValueArray; }
	
protected:
	
	Math::Float4x4* m_ValueArray;
};

// ---------------------------------------------------------------------------------------------------------------------
//	ITupleParameterAtomic
// ---------------------------------------------------------------------------------------------------------------------

template <typename TType>
class ITupleParameterAtomic : public IParameterAtomic
{
public:
	
	typedef TType				   C1Component;
	typedef Math::CTuple<TType, 2> C2Component;
	typedef Math::CTuple<TType, 3> C3Component;
	typedef Math::CTuple<TType, 4> C4Component;
	
public:
	
	ITupleParameterAtomic(uint16 _NumTuples=1);
	virtual ~ITupleParameterAtomic();
	
	CB_FORCEINLINE void CB_INLINE_ATTR SetValue(const C1Component& _rValue, uint16 _Index=0);
	CB_FORCEINLINE void CB_INLINE_ATTR SetValue(const C2Component& _rValue, uint16 _Index=0);
	CB_FORCEINLINE void CB_INLINE_ATTR SetValue(const C3Component& _rValue, uint16 _Index=0);
	CB_FORCEINLINE void CB_INLINE_ATTR SetValue(const C4Component& _rValue, uint16 _Index=0);
	
	CB_FORCEINLINE void CB_INLINE_ATTR SetValues(const C1Component* _pValues, uint16 _Count=1);
	CB_FORCEINLINE void CB_INLINE_ATTR SetValues(const C2Component* _pValues, uint16 _Count=1);
	CB_FORCEINLINE void CB_INLINE_ATTR SetValues(const C4Component* _pValues, uint16 _Count=1);
	
	const C4Component* GetValues() const { return m_ValueArray; }
	
protected:
	
	C4Component* m_ValueArray;
};

#include "CRL_ParameterAtomic.hpp"

typedef ITupleParameterAtomic<float>	IFloatParameterAtomic;
typedef ITupleParameterAtomic<int>		IIntParameterAtomic;
typedef ITupleParameterAtomic<bool>		IBoolParameterAtomic;

typedef CResourcePtr<IParameterAtomic>			 CParameterAtomicPtr;
typedef CResourcePtr<IMatrix4x4ParameterAtomic>  CMatrix4x4ParameterAtomicPtr;
typedef CResourcePtr<IFloatParameterAtomic>	 	 CFloatParameterAtomicPtr;
typedef CResourcePtr<IIntParameterAtomic>		 CIntParameterAtomicPtr;
typedef CResourcePtr<IBoolParameterAtomic>		 CBoolParameterAtomicPtr;

CB_SUBNAMESPACE_END

#endif